package com.bangcommunity.bbframe.sdm.web.shiro.token;

import com.bangcommunity.bbframe.common.utils.lang.StringUtils;
import com.bangcommunity.bbframe.sdm.utils.RequestUtils;
import com.bangcommunity.bbframe.sdm.web.exception.RestConf;
import com.bangcommunity.bbframe.sdm.web.shiro.SdmUidTokenAuthenticationToken;
import org.apache.shiro.authc.AuthenticationToken;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @author tanghc
 * @Date 17/11/16
 */
public class SdmTokenAuthenticationFilter<PK extends Serializable> extends AbstractTokenAuthenticationFilter {

    @Override
    protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest req = (HttpServletRequest)request;
        String tokenParam = RequestUtils.extractParam(req, RestConf.tokenKey);
        String uidParam = RequestUtils.extractParam(req, RestConf.uidKey);
        if (StringUtils.isBlank(tokenParam)) {
            logger.error("create sdm token fail no token");
            return null;
        }
        SdmUidTokenAuthenticationToken token = new SdmUidTokenAuthenticationToken();
        token.setSsoToken(tokenParam);
        token.setUid(uidParam);
        return token;
    }
}
